package goods_manage.show.view;

import goods_manage.entity.GoodsDo;
import goods_manage.handler.AddGoodsViewHandler;
import goods_manage.show.MainView;

import javax.swing.*;
import java.awt.*;

public class AddGoodsView extends JDialog {
    JPanel jPanel=new JPanel(new FlowLayout(FlowLayout.CENTER,10,20));
    JLabel nameLabel=new JLabel("商品名",JLabel.RIGHT);
    JTextField nameText=new JTextField();
    JLabel priceLabel=new JLabel("价格",JLabel.RIGHT);
    JTextField priceText=new JTextField();
    JLabel purchaseLabel =new JLabel("购进",JLabel.RIGHT);
    JTextField purchaseText =new JTextField();
    JLabel saleLabel=new JLabel("售出",JLabel.RIGHT);
    JTextField saleText=new JTextField();
    JLabel inventoryLabel=new JLabel("库存",JLabel.RIGHT);
    JTextField inventoryText=new JTextField();
    JLabel typeLabel =new JLabel("商品类型",JLabel.RIGHT);
    JTextField typeText =new JTextField();
    JButton addBtn=new JButton("添加");

    AddGoodsViewHandler addGoodsViewHandler;



    public AddGoodsView(MainView mainView){
        super(mainView,"添加商品",true);
        addGoodsViewHandler =new AddGoodsViewHandler(this,mainView);
        //添加组件
        nameLabel.setPreferredSize(new Dimension(80,30));
        nameText.setPreferredSize(new Dimension(200,30));
        jPanel.add(nameLabel);
        jPanel.add(nameText);

        priceLabel.setPreferredSize(new Dimension(80,30));
        priceText.setPreferredSize(new Dimension(200,30));
        jPanel.add(priceLabel);
        jPanel.add(priceText);
        purchaseLabel.setPreferredSize(new Dimension(80,30));
        purchaseText.setPreferredSize(new Dimension(200,30));
        jPanel.add(purchaseLabel);
        jPanel.add(purchaseText);
        saleLabel.setPreferredSize(new Dimension(80,30));
        saleText.setPreferredSize(new Dimension(200,30));
        jPanel.add(saleLabel);
        jPanel.add(saleText);
        inventoryLabel.setPreferredSize(new Dimension(80,30));
        inventoryText.setPreferredSize(new Dimension(200,30));
        jPanel.add(inventoryLabel);
        jPanel.add(inventoryText);
        typeLabel.setPreferredSize(new Dimension(80,30));
        typeText.setPreferredSize(new Dimension(200,30));
        jPanel.add(typeLabel);
        jPanel.add(typeText);


        addBtn.addActionListener(addGoodsViewHandler);
        jPanel.add(addBtn);
        //
        Container container=getContentPane();
        container.add(jPanel);


        setSize(350,500);
        setLocationRelativeTo(null);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);
        setResizable(false);
        setVisible(true);
    }

    public GoodsDo buildGoods(){
        GoodsDo goodsDo =new GoodsDo();
        goodsDo.setName(nameText.getText());
        goodsDo.setPrice(priceText.getText());
        goodsDo.setPurchase(purchaseText.getText());
        goodsDo.setSale(saleText.getText());   //!!!注意 JTextfield获取数据,需要用包装类转换
        goodsDo.setInventory(inventoryText.getText());
        goodsDo.setType(typeText.getText());
        return goodsDo;
    }
}
